package unsw.dao;

import unsw.model.TaskStatus;
import unsw.model.TransferTask;
import unsw.utils.DatabaseConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TransferTaskDao {
    private static final Connection conn;

    static {
        try {
            conn = DatabaseConnection.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void addTask(String fileName, String fromId, String toId, TaskStatus status) {
        String sql = "insert into transfertask(filename, fromId, toId, status) values(?,?,?,?)";

        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, fileName);
            pstmt.setString(2, fromId);
            pstmt.setString(3, toId);
            pstmt.setString(4, status.name());
            pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void updateTask(String fileName, String fromId, String toId, TaskStatus status) {
        String sql = "update  transfertask set status =? where filename =? and fromId =? and toId =?";

        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, status.name());
            pstmt.setString(2, fileName);
            pstmt.setString(3, fromId);
            pstmt.setString(4, toId);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public List<TransferTask> getTasks(TaskStatus taskStatus) {
        String sql = "select id, filename, fromId, toId, status from transfertask where status = ?  ";
        List<TransferTask> tasks = new ArrayList<>();
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, taskStatus.name());
            ResultSet resultSet = pstmt.executeQuery();
            while (resultSet.next()) {
                TransferTask task = new TransferTask();
                task.setId(resultSet.getInt("id"));
                task.setFileName(resultSet.getString("filename"));
                task.setFromId(resultSet.getString("fromId"));
                task.setToId(resultSet.getString("toId"));
                task.setStatus(resultSet.getString("status"));
                tasks.add(task);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return tasks;
    }

    public TransferTask getCurrentTask(String fileName, String fromId, String toId) {
        String sql = "select id, filename, fromId, toId, status from transfertask where fromId =? and toId =? and fileName =?";
        TransferTask task = new TransferTask();
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, fromId);
            pstmt.setString(2, toId);
            pstmt.setString(3, fileName);
            ResultSet resultSet = pstmt.executeQuery();
            if (resultSet.next()) {
                task.setId(resultSet.getInt("id"));
                task.setFileName(resultSet.getString("filename"));
                task.setFromId(resultSet.getString("fromId"));
                task.setToId(resultSet.getString("toId"));
                task.setStatus(resultSet.getString("status"));
            } else {
                return null;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return task;
    }


    public void deleteTransferTaskById(String id) {
        String sql = "delete from transfertask where fromId =? or toId =?";
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, id);
            pstmt.setString(2, id);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void deleteReadyTaskById(String fileName, String fromId, String toId) {
        String sql = "delete from transfertask where filename=? and fromId =? and toId =?";
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, fileName);
            pstmt.setString(2, fromId);
            pstmt.setString(3, toId);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
